package com.grj.parkingsystem.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.grj.parkingsystem.common.JwtUtil;
import com.grj.parkingsystem.common.Result;
import com.grj.parkingsystem.entity.Coupon;
import com.grj.parkingsystem.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/coupon")
@Api(tags = "6. 优惠券管理")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @Resource
    private JwtUtil jwtUtil;

    @GetMapping("/list")
    @ApiOperation(value = "获取优惠券列表", notes = "获取当前用户的可用优惠券列表")
    @ApiOperationSupport(order = 1)
    public Result<?> listCoupons(@RequestHeader("Authorization") String token) {
        List<Coupon> coupons = couponService.getAvailableCoupons(token);
        return Result.success(coupons);
    }

    @PostMapping("/use/{couponId}/{orderId}")
    @ApiOperation(value = "使用优惠券", notes = "用户使用优惠券支付停车订单")
    @ApiOperationSupport(order = 2)
    public Result<?> useCoupon(@ApiParam(value = "认证Token", required = true)
                             @RequestHeader("Authorization") String token,
                             @PathVariable Long couponId,
                             @PathVariable Long orderId) {
        couponService.useCoupon(token, couponId, orderId);
        return Result.success("使用成功");
    }

    @PostMapping("/receive/{couponTemplateId}")
    @ApiOperation(value = "领取优惠券", notes = "用户领取系统发放的优惠券")
    @ApiOperationSupport(order = 3)
    public Result<?> receiveCoupon(@ApiParam(value = "认证Token", required = true)
                                 @RequestHeader("Authorization") String token,
                                 @PathVariable Long couponTemplateId) {
        couponService.receiveCoupon(token, couponTemplateId);
        return Result.success("领取成功");
    }
}